<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/8/1
 **/

namespace app\admin\controller;


use app\admin\service\CronService;
use app\common\Annotation\ControllerAnnotation;
use app\common\Annotation\NodeAnotation;
use app\common\exception\ServiceException;
use think\facade\Request;
use app\common\controller\BackendController;

/**
 * @ControllerAnnotation(title="计划任务",auth=2)
 * Class CronController
 * @package app\admin\controller
 */
class CronController extends BackendController
{
    /**
     * @NodeAnotation(title="列表")
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (Request::isAjax()) {
            $result = CronService::getList(Request::param());
            return success_json_response("成功", $result['dataList'], ['count' => $result['total']]);
        } else {
            return $this->fetch();
        }
    }


    /**
     * @NodeAnotation(title="添加",auth=2)
     * @return mixed|\think\response\Json
     * @throws ServiceException
     */
    public function add()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = CronService::save($params);
            if ($res) {
                return success_json_response("添加成功", ["id" => $res]);
            } else {
                return json_response("添加失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改",auth=2)
     * @return mixed|\think\response\Json
     * @throws ServiceException
     */
    public function edit()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = CronService::save($params);
            if ($res) {
                return success_json_response("修改成功", ["id" => $res]);
            } else {
                return json_response("修改失败");
            }
        }
        $model = CronService::getByID($params['id']);
        $this->assign("model", $model);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除",auth=2)
     * @return \think\response\Json
     */
    public function del()
    {
        if (Request::isAjax()) {
            $res = CronService::del(Request::param());
            return $res ? success_json_response("删除成功") : json_response("删除失败");
        }
    }

    /**
     * @NodeAnotation(title="计划任务日志",auth=2)
     * @return \think\response\Json
     */
    public function log()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $result = CronService::getLogList(Request::param());
            return success_json_response("成功", $result['dataList'], ['count' => $result['total']]);
        } else {
            $this->assign("params", $params);
            return $this->fetch();
        }
    }


}